package com.foreverwin.mes.so.controller;

import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.so.dto.ShopOrderExpandReq;
import com.foreverwin.mes.so.dto.ShopOrderExpandResult;
import com.foreverwin.mes.so.service.ShopOrderExpandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Syngna
 * @since 2020-02-06
 */
@Controller
@RequestMapping("/shopOrderExpands")
public class ShopOrderExpandController {
    private final Logger logger = LoggerFactory.getLogger(ShopOrderExpandController.class);

    @Autowired
    public ShopOrderExpandService shopOrderExpandService;

    /**
     * 需求展工单
     *
     * @param shopOrderExpandReq 传递的实体
     * @return null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_expand")
    public AjaxResult expand(@RequestBody ShopOrderExpandReq shopOrderExpandReq) {
        ShopOrderExpandResult shopOrderExpandResult;
        String site = CommonMethods.getSite();
        shopOrderExpandReq.setSite(site);
        try {
            shopOrderExpandResult = shopOrderExpandService.doExpand(shopOrderExpandReq);
        } catch (Exception e) {
            logger.error("expand -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(shopOrderExpandResult);
    }

    /**
     * 工单开立
     *
     * @param shopOrderExpandResultList
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_generateShopOrder")
    public AjaxResult generateShopOrder(@RequestBody List<ShopOrderExpandResult> shopOrderExpandResultList) {
        List<ShopOrderExpandResult> resultList;
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        try {
            resultList = shopOrderExpandService.generateShopOrder(site, user, shopOrderExpandResultList);
        } catch (Exception e) {
            logger.error("generateShopOrder -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }
}